package com.xxxy.hyc.xyxmp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import com.xxxy.hyc.xyxmp.R;
import com.xxxy.hyc.xyxmp.adapter.GoodsAdapter;
import com.xxxy.hyc.xyxmp.bean.Goods;
import com.xxxy.hyc.xyxmp.utils.UserSessionManager;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private GoodsAdapter adapter;
    private List<Goods> goodsList = new ArrayList<>();
    private Button btnLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 检查用户是否已登录，如果未登录则跳转到登录页面
        if (!UserSessionManager.getInstance().isLoggedIn()) {
            Intent loginIntent = new Intent(this, LoginActivity.class);
            startActivity(loginIntent);
            finish();
            return;
        }
        
        setContentView(R.layout.activity_main2);

        // 显示返回按钮（主页面通常不需要，但为了示例保留）
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false); // 主页面不显示返回按钮
        }

        btnLogout = findViewById(R.id.btn_logout);
        btnLogout.setOnClickListener(v -> {
            UserSessionManager.getInstance().logout();
            Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
            loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(loginIntent);
            finish();
        });

        // 初始化商品数据
        initGoodsData();

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.rv_goods);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); // 2列网格
        adapter = new GoodsAdapter(goodsList, this);
        recyclerView.setAdapter(adapter);

        // 点击商品跳转到详情页（暂时注释，等创建GoodsDetailActivity后再打开）
        adapter.setOnItemClickListener(position -> {
            Goods goods = goodsList.get(position);
             Intent intent = new Intent(MainActivity.this, GoodsDetailActivity.class);
             intent.putExtra("goods_id", goods.getId());
             startActivity(intent);
        });
    }

    // 模拟商品数据
    private void initGoodsData() {
        goodsList.add(new Goods(1, "可乐", 3.0, 100, "cola", "冰镇可乐330ml"));
        goodsList.add(new Goods(2, "薯片", 5.0, 50, "chips", "原味薯片100g"));
        goodsList.add(new Goods(3, "矿泉水", 2.0, 200, "water", "天然矿泉水550ml"));
        goodsList.add(new Goods(4, "巧克力", 8.0, 30, "chocolate", "牛奶巧克力50g"));
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}